//
//  SYJVisitorView.swift
//  weibo
//
//  Created by 孙英建 on 16/4/16.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit

class SYJVisitorView: UIView {

    var clickButton: (() -> ())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUI()
        setCircleImageViewTransportRotation()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    // MARK: - 按钮的点击事件
    @objc private func loginClick(){
        clickButton?()
    }
    
    // MARK: - 定义一个外部调用的方法
    func setVisitorInfo(imgName: String?, titleName: String?){
        
        if let img = imgName, title = titleName {
            iconImageView.image = UIImage(named: img)
            messageLabel.text = title
            circleImageView.hidden = true
        }else{
            circleImageView.hidden = false
        }
        
    }
    
    
    // MARK: - 让圆圈图片选装
    func setCircleImageViewTransportRotation(){
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.duration = 10
        animation.toValue = 2 * M_PI
        animation.repeatCount = MAXFLOAT
        
        animation.removedOnCompletion = false
        
        circleImageView.layer.addAnimation(animation, forKey: nil)
    }
    
    // MARK: - 添加控件设置UI
    func setUI(){
        
        self.backgroundColor = UIColor(red: 237.0/255.0, green: 237.0/255.0, blue: 237.0/255.0, alpha: 1)
        
        addSubview(circleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registButton)
        addSubview(loginButton)
        
        // 添加约束
        circleImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self)
        }
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
            make.centerX.equalTo(maskImageView)
            make.width.equalTo(230)
            make.height.equalTo(36)
        }
        
        registButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
        
    }
    
    
    // MARK: - 懒加载控件
    
    // 圆圈
    private lazy var circleImageView: UIImageView = {
        let img = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        return img
    }()
    
    // 遮挡
    private lazy var maskImageView: UIImageView = {
        let maskImgView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return maskImgView
    }()
    
    // 小房子
    private lazy var iconImageView: UIImageView = {
        let iconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return iconView
    }()
    
    // 文字
    private lazy var messageLabel: UILabel = {
       let messageLabel = UILabel()
        
        messageLabel.textColor = UIColor.grayColor()
        messageLabel.numberOfLines = 0
        messageLabel.textAlignment = NSTextAlignment.Center
        messageLabel.font = UIFont.systemFontOfSize(14)
        messageLabel.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        return messageLabel
    }()
    
    // 注册按钮
    private lazy var registButton: UIButton = {
        
        let registBtn = UIButton()
        
        registBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        registBtn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        registBtn.setTitle("注册", forState: UIControlState.Normal)
        registBtn.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        // 点击事件
        registBtn.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
       
        return registBtn
    }()
    
    // 登录按钮
    private lazy var loginButton: UIButton = {
        
        let loginBtn = UIButton()
        
        loginBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        loginBtn.setTitle("登录", forState: UIControlState.Normal)
        loginBtn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        loginBtn.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        // 点击事件
        loginBtn.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return loginBtn
    }()
    
}







